//
//  HomingMissileShotObject.m
//  Agility
//
//  Created by Dominique d'Argent on 18.03.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "HomingMissileShotObject.h"
#import "ContrailObject.h"
#import "GameObjectLayer.h"

#define CONTRAIL_COOLDOWN 0.15

@implementation HomingMissileShotObject

+ (NSString *)imageName {
    return @"homingMissileShot";
}

- (id)initWithPosition:(CGPoint)newPosition target:(GameObject *)newTarget trajectory:(CGFloat)newTrajectory {
    self = [super initWithPosition:newPosition target:newTarget trajectory:newTrajectory];
    
    if (self) {
        maxLifeTime *= 2.0;
        angularVelocity /= 4.0;
        contrailTimeout = CONTRAIL_COOLDOWN;
        angle = trajectory;
    }

    return self;
}

- (void)updatePositionWithTimeInterval:(NSTimeInterval)timeInterval {
    angle = [self angleToPoint:target.position];
    durationForRotation = [self durationForRotationToAngle:angle];
    
    trajectory = self.currentAngle;
    durationForMovement = 0;
    
    contrailTimeout -= timeInterval;
    if (contrailTimeout <= 0) {
        CGFloat currentAngle = self.currentAngle;
        
        CGPoint contrailPosition = position;
        contrailPosition.x -= layer.size.width/2  * sinf(currentAngle);
        contrailPosition.y -= layer.size.height/2 * cos(currentAngle);
        
        [ContrailObject spawnAtPosition:contrailPosition];
        
        contrailTimeout = CONTRAIL_COOLDOWN;
    }
    
    [super updatePositionWithTimeInterval:timeInterval];
}

- (void)updateTransformWithTimeInterval:(NSTimeInterval)timeInterval {
}

@end
